/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.slice;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.core.datamodel.Scalar;
import com.cv4j.core.pixels.NormRotate;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.element.PixelMapElement;
import ohos.media.image.PixelMap;

/**
 * RotateAbilitySlice
 *
 * @since 2021-05-14
 */
public class RotateAbilitySlice extends BaseAbilitySlice {
    private Image image;
    private Image resultimage;

    /**
     * getUIContentLayoutId
     *
     * @return int
     */
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_rotate;
    }

    /**
     * 操作类
     *
     * @param intent 意图
     */
    protected void initComponents(Intent intent) {
        initView();
        initData();
    }

    private void initData() {
        image.setPixelMap(ResourceTable.Media_pixel_test_1);
        CV4JImage cv4jImage = new CV4JImage(image.getPixelMap());
        ImageProcessor imageProcessor = cv4jImage.getProcessor();
        NormRotate normRotate = new NormRotate();
        final float width = 120f;
        final int rgbs = 255;
        imageProcessor = normRotate.rotate(imageProcessor, width, Scalar.rgb(rgbs, 0, 0));
        if (imageProcessor != null) {
            CV4JImage resultcv4jImage = new CV4JImage(imageProcessor.getWidth(),
                imageProcessor.getHeight(), imageProcessor.getPixels());
            PixelMap pixelMap = resultcv4jImage.getProcessor().getImage().toBitmap();
            resultimage.setImageElement(new PixelMapElement(pixelMap));
        }
    }

    private void initView() {
        image = (Image) findComponentById(ResourceTable.Id_rotate_image);
        resultimage = (Image) findComponentById(ResourceTable.Id_rotate_image1);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
